//
//  UIButton+Extension.swift
//  YouYouDriver
//
//  Created by Apple on 2017/8/31.
//  Copyright © 2017年 The_X. All rights reserved.
//

import UIKit

extension UIButton {

    convenience init(es_title: String? = "",
                     titleColor: UIColor = UIColor.darkText,
                     highliColor: UIColor = UIColor.lightGray,
                     backColor: UIColor = UIColor.white,
                     font: CGFloat = 15,
                     target: Any? = nil,
                     action: Selector? = nil) {
        
        self.init()
        
        self.setTitle(es_title, for: .normal)
        self.setTitleColor(titleColor, for: .normal)
        self.setTitleColor(highliColor, for: .highlighted)
        self.backgroundColor = backColor
        self.titleLabel?.font = UIFont.systemFont(ofSize: font)
        
        if let es_target = target,
            let es_action = action{
            
            self.addTarget(es_target, action: es_action, for: .touchUpInside)
        }
        
        self.sizeToFit()
    }
    
    
    convenience init(es_image: String? = nil,
                     backImage: String? = nil,
                     target: Any? = nil,
                     action: Selector? = nil) {
        
        self.init()
        
        if let es_image = es_image {
            
            self.setImage(UIImage(named: es_image), for: .normal)
            self.setImage(UIImage(named: es_image + "_selected"), for: .selected)
        }
        
        if let backImage = backImage {
            
            self.setBackgroundImage(UIImage(named: backImage), for: .normal)
            self.setBackgroundImage(UIImage(named: backImage  + "_highlighted"), for: .highlighted)
        }
        
        if let es_target = target,
            let es_action = action{
            
            self.addTarget(es_target, action: es_action, for: .touchUpInside)
        }
        
        self.sizeToFit()
    }
    
    convenience init(es_image: String,
                     title: String,
                     color: UIColor = UIColor.darkText,
                     margin: CGFloat = 0,
                     target: Any? = nil,
                     action: Selector? = nil) {
        self.init()
        
        self.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        self.setImage(UIImage(named: es_image), for: .normal)
        self.setTitle(title, for: .normal)
        self.setTitleColor(color, for: .normal)
        
        self.imageEdgeInsets = UIEdgeInsets(top: -(titleLabel!.intrinsicContentSize.height+12), left: 0, bottom: 0, right: -(titleLabel!.intrinsicContentSize.width))
        self.titleEdgeInsets = UIEdgeInsets(top: titleLabel!.intrinsicContentSize.height + margin, left: -(imageView!.frame.width), bottom: 0, right: 0)
        
        if let es_target = target,
            let es_action = action{
            
            self.addTarget(es_target, action: es_action, for: .touchUpInside)
        }
    }
}
